package com.balarawool.continuations.platformthread;

import static com.balarawool.continuations.ExecutorUtils.newStopCommandThread;

public class PlatformThreadDemo {
    public static final Scheduler SCHEDULER = new Scheduler();

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {

            Runnable r1 = () -> {
                System.out.println("1.1");
                System.out.println("1.2");
                try {
                    WaitingOperation.perform("Network", 2);
                } catch (InterruptedException e) {
                    System.out.println(Thread.currentThread() + " executing interrupted");
                }
                System.out.println("1.3");
                System.out.println("1.4");
            };
            Runnable r2 = () -> {
                System.out.println("2.1");
                System.out.println("2.2");
                try {
                    WaitingOperation.perform("DB", 5);
                } catch (InterruptedException e) {
                    System.out.println(Thread.currentThread() + " executing interrupted");
                }
                System.out.println("2.3");
                System.out.println("2.4");
            };
            SCHEDULER.schedule(r1);
            SCHEDULER.schedule(r2);

        }

        // 此线程的作用：监听控制台输入的命令，当输入stop、quit、exit时，停止虚拟线程调度器。
        // 例如，任务执行完成后，停止整个程序
        Thread stopThread = newStopCommandThread(SCHEDULER::stop);
        stopThread.start();
    }
}
